package cn.liuxiany.alibaba.job.jobhandler;

import cn.liuxiany.alibaba.job.service.JobService;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 * @Description job测试demo
 * @Author liuxiany
 * @Date 2023/11/09
 */
@Slf4j
@Component
public class MyJob {

    @Autowired
    private JobService jobService;

    /**
     * 1、简单任务示例（Bean模式）
     */
    @XxlJob("myJobHandler")
    public void demoJobHandler() throws Exception {
        XxlJobHelper.log("XXL-JOB, My Jack:Hello World.");

        for (int i = 0; i < 5; i++) {
            XxlJobHelper.log("beat at:" + i);
            TimeUnit.SECONDS.sleep(2);
        }
        // default success
    }

    /**
     * 测试连接数据库（Bean模式）
     */
    @XxlJob("myJobDbHandler")
    public void demoJobDbHandler() throws Exception {
        String param = XxlJobHelper.getJobParam();
        XxlJobHelper.log("XXL-JOB-DB, My Jack:Hello World." + param);

        jobService.test(param);
    }
}
